package edu.hitwh.controller;

import edu.hitwh.entity.Transaction;
import edu.hitwh.record.TransactionRecord;
import edu.hitwh.Repository.TransactionQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import edu.hitwh.service.TransactionService;
import edu.hitwh.wrapper.Resp;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;

import org.springframework.data.domain.Sort;
import edu.hitwh.entity.BorrowTransactionStatus;
import java.sql.Timestamp;

@RestController
@RequestMapping("/transaction")
@RateLimiter(name = "default-ratelimiter")
@Bulkhead(name = "default-bulkhead")
@CircuitBreaker(name = "default-circuitbreaker")
public class TransactionController {
    private final TransactionService transactionService;

    public TransactionController(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @PostMapping
    public Resp<Boolean> create(@RequestBody TransactionRecord transaction) {
        transactionService.create(transaction.toEntity());
        return Resp.success(true);
    }

    @PostMapping("/{id}/close")
    public Resp<Boolean> close(@PathVariable Integer id) {
        transactionService.close(id);
        return Resp.success(true);
    }

    @GetMapping("/search")
    public Resp<Page<Transaction>> search(
            @RequestParam(required = false) String bookTitle,
            @RequestParam(required = false) String bookAuthor,
            @RequestParam(required = false) String bookIsbn,
            @RequestParam(required = false) String borrowerName,
            @RequestParam(required = false) String borrowerEmail,
            @RequestParam(required = false) BorrowTransactionStatus status,
            @RequestParam(required = false) String borrowDateStart,
            @RequestParam(required = false) String borrowDateEnd,
            @RequestParam(required = false) String returnDateStart,
            @RequestParam(required = false) String returnDateEnd,
            @PageableDefault(size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable
    ) {
        TransactionQuery query = TransactionQuery.builder()
                .bookTitle(bookTitle)
                .bookAuthor(bookAuthor)
                .bookIsbn(bookIsbn)
                .borrowerName(borrowerName)
                .borrowerEmail(borrowerEmail)
                .status(status)
                .borrowDateStart(borrowDateStart != null && !borrowDateStart.isEmpty() ? Timestamp.valueOf(borrowDateStart) : null)
                .borrowDateEnd(borrowDateEnd != null && !borrowDateEnd.isEmpty() ? Timestamp.valueOf(borrowDateEnd) : null)
                .returnDateStart(returnDateStart != null && !returnDateStart.isEmpty() ? Timestamp.valueOf(returnDateStart) : null)
                .returnDateEnd(returnDateEnd != null && !returnDateEnd.isEmpty() ? Timestamp.valueOf(returnDateEnd) : null)
                .build();
        
        return Resp.success(transactionService.search(query, pageable));
    }
}